<template>
  <q-page padding>
    <div class="row q-gutter-md items-center" style="margin-bottom: 10px">
      <q-input
        style="width: 20%"
        v-model="queryParams.api_group"
        :label="$t('Api') + $t('Group')"
      />
      <q-input
        style="width: 20%"
        v-model="queryParams.api_method"
        :label="$t('Api') + $t('Method')"
      />
      <q-btn color="primary" @click="handleSearch" :label="$t('Search')" />
      <q-btn color="primary" @click="resetSearch" :label="$t('Reset')" />
    </div>

    <q-table
      dense
      row-key="id"
      separator="cell"
      :rows="tableData"
      :columns="columns"
      v-model:pagination="pagination"
      :rows-per-page-options="pageOptions"
      :loading="loading"
      @request="onRequest"
    >
      <template v-slot:top="props">
        <q-btn
          color="primary"
          @click="showAddForm()"
          :label="$t('Add') + ' ' + $t('Api')"
        />
        <q-space />
        <q-btn
          flat
          round
          dense
          :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
          @click="props.toggleFullscreen"
          class="q-ml-md"
        />
      </template>

      <template v-slot:body-cell-status="props">
        <q-td :props="props">
          <DcDictCell dictName="statusOnOff" :dictCode="props.row.status" />
        </q-td>
      </template>

      <template v-slot:body-cell-source="props">
        <q-td :props="props">
          <DcDictCell dictName="source" :dictCode="props.row.source" />
        </q-td>
      </template>

      <template v-slot:body-cell-actions="props">
        <q-td :props="props">
          <div class="q-gutter-xs">
            <q-btn
              color="primary"
              @click="showEditForm(props.row)"
              :label="$t('Edit')"
            />
            <q-btn
              color="negative"
              @click="handleDelete(props.row)"
              :label="$t('Delete')"
            />
          </div>
        </q-td>
      </template>
    </q-table>
    <recordDetail ref="recordDetailDialog" @handleFinish="handleFinish" />
  </q-page>
</template>

<script setup>
import useTableData from "src/composables/useTableData";
import { computed, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import recordDetail from "./ApiRecordDetail.vue";

const { t } = useI18n();
const url = {
  list: "api/get-api-list",
  delete: "api/delete-api-by-id",
};
const columns = computed(() => {
  return [
    { name: "sort", align: "center", label: t("Sort"), field: "sort" },
    {
      name: "api_group",
      align: "center",
      label: t("Api") + t("Group"),
      field: "api_group",
    },
    {
      name: "api_method",
      align: "center",
      label: t("Api") + t("Method"),
      field: "api_method",
    },
    {
      name: "api_path",
      align: "center",
      label: t("Api") + t("Path"),
      field: "api_path",
    },
    { name: "memo", align: "center", label: t("Memo"), field: "memo" },
    { name: "status", align: "center", label: t("Status"), field: "status" },
    { name: "source", align: "center", label: t("Source"), field: "source" },
    { name: "actions", align: "center", label: t("Actions"), field: "actions" },
  ];
});
const {
  pagination,
  queryParams,
  pageOptions,
  DcDictCell,
  loading,
  tableData,
  recordDetailDialog,
  showAddForm,
  showEditForm,
  onRequest,
  getTableData,
  handleSearch,
  resetSearch,
  handleFinish,
  handleDelete,
} = useTableData(url);

onMounted(() => {
  getTableData();
});
</script>

<script>
import { defineComponent } from "vue";

export default defineComponent({
  name: "ApiComp",
});
</script>
